<!DOCTYPE html>
<html lang="zh-CN">
<!-- 某场比赛的历史赔率 -->

<head>
    <meta charset="UTF-8" />
    <title>红牌比赛历史</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            /* 水平居中 */
            align-items: flex-start;
            /* 顶部对齐 */
            min-height: 100vh;
            /* 保证全屏背景 */
            padding-top: 20px;
            /* 顶部留空 */
            background: #f0f2f5;
        }

        table {
            border-collapse: collapse;
            width: 95%;
            max-width: 1000px;
            background: #ffffff;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            overflow: hidden;
            text-align: center;
        }

        th,
        td {
            border: 1px solid #e0e0e0;
            /* padding: 12px 8px; */
            font-size: 14px;
        }

        th {
            background: #004aad;
            color: #ffffff;
            font-weight: 600;
        }

        tr:hover {
            background-color: #f5f5f5;
        }

        caption {
            caption-side: top;
            font-size: 20px;
            font-weight: bold;
            padding: 12px;
            color: #333;
        }
    </style>
</head>

<body>
    <table>
        <thead>
            <tr>
                <th>时间</th>
                <th>联赛</th>
                <th>比赛</th>
                <th>详情</th>
                <th>删除</th>
            </tr>
        </thead>
        <tbody id="list">
            <!-- 可继续添加更多行 -->
        </tbody>
    </table>
</body>
<script src="/resources/common.js"></script>
<script>
    const params = new URLSearchParams(window.location.search)
    // 获取某个参数
    const id = params.get('id')
    const socket = new WebSocket(`ws://${IP}:8081`);
    socket.addEventListener("open", function (event) {
        socket.send(JSON.stringify({
            type: "red_history",
            data: id

        }))
    });
    socket.addEventListener("message", function (event) {
        let data = JSON.parse(event.data)
        if (data.type == "red_history") {
            red_history(data)
        }
    });

    socket.addEventListener("close", function (event) {
    })

    function red_history(data) {
        let home, away;
        let tr = ``
        for (let i = 0; i < data.data.length; i++) {
            let time = formatTimestampMs(data.data[i].time.split('.')[0])
            tr = tr + ` <tr>
                            <td>${time}</td> 
                            <td>${data.data[i].league}</td>
                            <td>${data.data[i].matchName}</td>
                            <td><a href="/odds.html?id=${data.data[i].matchId}">赔率列表</a></td>
                            <td><button onclick="deleteRedHistory('${data.data[i].id}')">删除</button></td>
                        </tr>`
        }
        document.getElementById("list").insertAdjacentHTML('beforeend', tr)
    }


    function formatTimestampMs(ts) {
        const date = new Date(parseInt(ts))
        return date.getFullYear() + '-' +
            String(date.getMonth() + 1).padStart(2, '0') + '-' +
            String(date.getDate()).padStart(2, '0') + ' ' +
            String(date.getHours()).padStart(2, '0') + ':' +
            String(date.getMinutes()).padStart(2, '0') + ':' +
            String(date.getSeconds()).padStart(2, '0') + '.' +
            String(date.getMilliseconds()).padStart(3, '0')
    }

    function deleteRedHistory(id) {
        if (confirm("确定删除该红牌历史记录吗？")) {
            socket.send(JSON.stringify({
                type: "delete_red_history",
                data: id
            }))
            location.reload()
        }
    }
</script>

</html>